<?php

namespace Buu\Modules\Constant\Controllers;

use Adldap\Models\ModelNotFoundException;
use App\Models\ConstantType;
use App\Models\ConstantValue;
use App\Models\ConstantValueGroup;
use App\Models\ConstantValueGroupRelation;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Encore\Admin\Controllers\AdminController;
use Illuminate\Http\Request;
use Illuminate\Support\MessageBag;


class ConstantValueGroupRelationController extends AdminController
{
    /**
     * title
     * @return string
     */
    protected function title()
    {
        return "常量组成员关系表";
    }

    /**
     * 列表构建
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new ConstantValueGroupRelation());
        $grid->disableFilter();
        $grid->disableExport();
        $grid->disableBatchActions();
        $grid->disablePagination();

        $grid->column('constant_type_id', '常量类型')->display(function () {
            return ConstantType::where('id',$this->constant_type_id)->pluck('name');
        })->label();
        $grid->column('constant_value_group_id', '常量组')->display(function () {
            return ConstantValueGroup::where('id',$this->constant_value_group_id)->pluck('name');
        })->label();
        $grid->column('constant_value_id', '常量值')->display(function () {
            return ConstantValue ::where('id',$this->constant_value_id)->pluck('name');
        })->label();
        $grid->disableActions();

        return $grid;
    }

    public function addnew(Request $request, Content $content, $iCid,$iGid)
    {
        $request = $request->all();
        return $content
            ->header('添加组内常量')
            ->breadcrumb(
                ['text' => '常量列表', 'url' => '/Constant/constant-type/list'],
                ['text' => '添加组内常量']
            )
            ->body(self::addAction($request, $iCid,$iGid));
    }

    public function addAction($request, $iCid,$iGid)
    {
        $aCtype = ConstantType::getOne($iCid)->toArray();
        $aCgroup = ConstantValueGroup::getOne($iGid)->toArray();

        $form = new Form(new ConstantValueGroup());
//        $form->model()->where('status',1);
        $form->html("<p>常量类型：".$aCtype['name'] .'</p><p>常量组：'.$aCgroup['name']."</p>" );



        $form->hidden('constant_type_id', '选择常量类型接口名')->value($iCid);
        $form->hidden('constant_value_group_id', '选择常量类型接口名')->value($iGid);


//        $form->multipleSelect('constant_value_id', '常量类型')
//            ->options(ConstantValue::where('constant_type_id',$iCid)->pluck('name', 'id'))->required()->width('300px');


        $aRelation = ConstantValueGroupRelation::where('constant_type_id', $iCid)->where('constant_value_group_id',$iGid)->where('status',1)->pluck('constant_value_id')->toArray();

//        $form->multipleSelect('constant_value_ids', '常量类型')
//            ->options(
//                ConstantValue::where('constant_type_id',$iCid)->pluck('name', 'id')
//            )->required()->width('300px')->default($aRelation);




        $form->multipleSelect('constant_value_ids', '常量类型')
            ->options(
                function () use ($iCid) {
                    $aResCantValue = [];
                    $aCantValue = ConstantValue::where('constant_type_id', $iCid)->select('desc','name', 'id')->get()->toArray();

                    foreach ($aCantValue as $value){
                        $aResCantValue[$value['id']]  = $value['name'].'-' .$value['desc'];
                    }

                    return $aResCantValue;



                }
            )->required()->width('300px')->default($aRelation);

        $form->tools(function (Form\Tools $tools) {
            // 去掉`列表`按钮
            $tools->disableList();

            // 去掉`删除`按钮
            $tools->disableDelete();

            // 去掉`查看`按钮
            $tools->disableView();
        });

        $form->footer(function ($footer) {
            // 去掉`查看`checkbox
            $footer->disableViewCheck();

            // 去掉`继续编辑`checkbox
            $footer->disableEditingCheck();

            // 去掉`继续创建`checkbox
            $footer->disableCreatingCheck();

        });
        $form->setAction('/admin/Constant/constant-value-group-relation/addsave');
        return $form;
    }

    public function addsave(Request $request)
    {



        $request = $request->all();

        $aInsertData = [];


//
        foreach ($request['constant_value_ids'] as $constant_type_id){
            if(!empty($constant_type_id)){

                $aInsertData[] = [
                    'constant_type_id'        => $request['constant_type_id'],
                    'constant_value_group_id' => $request['constant_value_group_id'],
                    'constant_value_id'       => $constant_type_id,
                ];
            }
        }

        if(!empty($aInsertData)){
            //删除
            ConstantValueGroupRelation::where(['constant_type_id'=>$request['constant_type_id'],'constant_value_group_id'=>$request['constant_value_group_id']])->update(['status'=>9]);
            //添加
            if( ConstantValueGroupRelation::insert($aInsertData)){
                $sMsg = 'success';
            }else{
                $sMsg = 'error';
            }

            admin_toastr($sMsg, $sMsg, ['timeOut' => 3]);
        }

        return redirect('/admin/Constant/constant-value-group/list/'.$request['constant_type_id']);
    }


}